<!--我的订单页面组件
 -->
<template>
  <div class="order">
    <!-- 我的订单头部 -->
    <div class="order-header">
      <div class="order-header-content">
        <p>
          <i class="el-icon-s-order" style="font-size: 30px;color: #ff6700;"></i>
          我的订单
        </p>
      </div>
    </div>
    <!-- 我的订单头部END -->

    <!-- 我的订单主要内容 -->
    <div class="order-content" v-if="orders.length>0">
      <div class="content" v-for="(item,index) in orders" :key="index">
        <ul>
          <!-- 我的订单表头 -->
          <li class="order-info">
            <div class="order-id">订单编号: {{item.orderId}}</div>
            <div class="order-time">订单时间: {{item.createTime | dateFormat}}</div>
            <!-- <div class="order-set" v-show="item.status == '已完成'">
              <el-button  type="success" @click="commentDialog(item.orderItemReturns)" >评价</el-button>
            </div> -->
            <div class="order-set" v-show="item.status == '未完成'">
              <el-button  @click="deleteOrder(item)" type="danger">取消订单</el-button>
              <el-button  @click="showdiable(item.orderId)"   type="success">去支付</el-button>
            </div>
          </li>
          <li class="header">
            <div class="pro-img"></div>
            <div class="pro-name">商品名称</div>
            <div class="pro-address">收货地址</div>
             <div class="pro-price">姓名</div>
            <div class="pro-price">手机号</div>
            <div class="pro-price">单价</div>
           
            <div class="pro-num">数量</div>
            <div class="pro-status">发货状态</div>
            <!-- <div class="pro-edit" >操作</div> -->
          </li>
          <!-- 我的订单表头END -->

          <!-- 订单列表 -->
          <li class="product-list" v-for="(product,i) in item.orderItemReturns" :key="i">
            <div class="pro-img">
              <router-link :to="{ path: '/goods/details', query: {productID:product.spuid} }">
                <img :src="product.skuImg" />
              </router-link>
            </div>
            <div class="pro-name">
              <router-link
                :to="{ path: '/goods/details', query: {productID:product.spuid} }"
              >{{product.skuName}}</router-link>
            </div>
            <div class="pro-address">{{item.userAddressEntity.city}}{{item.userAddressEntity.detailAddress}}</div>
            <div class="pro-price">{{item.userAddressEntity.username}}</div>
            <div class="pro-price">{{item.userAddressEntity.telephone}}</div>
            <div class="pro-price">{{product.price}}元</div>
            
            <div class="pro-num">{{product.count}}</div>
            <div class="pro-status">{{item.logisticsStatus}}</div>
            <div class="pro-edit" v-show="item.logisticsStatus == '已发货'">
              <span   @click="commentDialog(product)" >评价</span>
            </div>
          </li>
        </ul>
        <div class="order-bar">
          <div class="order-bar-left">
            <span class="order-total">
              共
              <span class="order-total-num">{{item.orderItemReturns.length}}</span> 件商品
            </span>
          </div>
          <div class="order-bar-right">
            <span>
              <span class="total-price-title">合计：</span>
              <span class="total-price">{{item.price}}元</span>
            </span>
          </div>
          <!-- 订单列表END -->
        </div>
      </div>
      <div style="margin-top:-40px;"></div>
    </div>
    <!-- 我的订单主要内容END -->

    <!-- 订单为空的时候显示的内容 -->
    <div v-else class="order-empty">
      <div class="empty">
        <h2>您的订单还是空的！</h2>
        <p>快去购物吧！</p>
      </div>
    </div>
    <!-- 订单为空的时候显示的内容END -->
     <!-- 支付弹框 -->
    <el-dialog
      title="请用支付宝扫码支付"
      :visible.sync="payDialogVisible"
      width="18%"
      center>
      <div>
        <img src="../assets/imgs/erweima.jpg" style="height=200px;width:200px" alt="">
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="payDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="bought">我已支付</el-button>
      </span>
    </el-dialog>
     <!-- 发表评价弹框 -->
    <el-dialog title="请评价商品" :visible.sync="commentVisible" width="40%" center>
      <el-form :model="commentInfo" label-width="100px" class="demo-ruleForm">
        <el-form-item label="等级" prop="description">
          <el-radio-group v-model="commentInfo.star" size="medium">
            <el-radio-button label="1">1<i class="el-icon-star-on"></i></el-radio-button>
            <el-radio-button label="2">2<i class="el-icon-star-on"></i></el-radio-button>
            <el-radio-button label="3">3<i class="el-icon-star-on"></i></el-radio-button>
            <el-radio-button label="4">4<i class="el-icon-star-on"></i></el-radio-button>
            <el-radio-button label="5">5<i class="el-icon-star-on"></i></el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="内容">
          <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            v-model="commentInfo.description"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="commentVisible = false">取 消</el-button>
        <el-button type="primary" @click="addComment">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import cookie from 'js-cookie'
export default {
  data() {
    return {
      // 订单列表 
      orders: [], 
      payDialogVisible:false,
      // 去支付的订单号
      clickorderId:'',
       // 评价的内容
      commentInfo: {
        star: 5,
        description: "",
      },
      commentVisible: false,
    };
  },
  created(){
    this.getOrder()
  },
  activated() {
    // 获取订单数据
    this.getOrder()
  },
  watch: {
    // 通过订单信息，计算出每个订单的商品数量及总价
    // orders: function(val) {
    //   let total = [];
    //   for (let i = 0; i < val.length; i++) {
    //     const element = val[i];

    //     let totalNum = 0;
    //     let totalPrice = 0;
    //     for (let j = 0; j < element.length; j++) {
    //       const temp = element[j];
    //       totalNum += temp.product_num;
    //       totalPrice += temp.product_price * temp.product_num;
    //     }
    //     total.push({ totalNum, totalPrice });
    //     console.log(total)
    //   }
    //   this.total = total;
    // }
  },
  methods: {
     async getOrder(){
       const{data:res} = await this.$http.get("/xuanmall/order/list?")
       console.log("订单数据",res)
       if(res.code !==0)return this.$message.error("订单列表获取失败")
      this.orders  = res.page.list
    },
    // 取消订单
    async  deleteOrder(data){
      console.log("取消订单的id",data)  
      //  data.orderItemReturns.forEach(async (item) => {
      //   const { data: res1 } = await this.$http.post("/xuanmall/skumessage/update", {
      //     skuId: item.skuid,
      //     stockCount: item.stockCount + item.count,
      //   });
      //   if (res1.code !== 0) return;
      // });
      this.getOrder()
      this.$message.success("订单取消成功")
    },
    // 显示支付弹框
     showdiable(id){
      this.payDialogVisible = true
      this.clickorderId = id
    },
    // 支付完成
     async bought(){
        const {data:res}  =await this.$http.post("/xuanmall/order/update",{
          orderId:this.clickorderId,
          status:"已完成"
        })
      if(res.code !==0)return this.$message.error("支付失败")
      this.getOrder()
      this.$message.success("支付成功")
       this.payDialogVisible = false
    },
    // 评价弹框显示
    commentDialog(data) {
      console.log('data',JSON.parse(cookie.get("user")).id);
      // this.comment = data;
      (this.commentInfo.skuId = data.skuid),
        (this.commentInfo.spuId = data.spuid),
        (this.commentInfo.parentId = 0),
        (this.commentInfo.userId = JSON.parse(cookie.get("user")).id),
        (this.commentInfo.commentType = 0),
        (this.commentInfo.description = ""),
        (this.commentInfo.resources = ""),
        (this.commentInfo.likesCount = 0),
        (this.commentInfo.replyCount = 0),
        (this.commentInfo.star = 5);
      this.commentVisible = true;
    },
    // 发表评价
    async addComment() {
      const { data: res } = await this.$http.post(
        `/xuanmall/shopcomment/save`,
        this.commentInfo
      );
      // console.log(res)
      if (res.code !== 0) {
        this.$message.error("评价失败");
      }
      this.$message.success("评价成功");
      this.commentVisible = false;
    },
  },
};
</script>
<style scoped>
.order {
  background-color: #f5f5f5;
  padding-bottom: 20px;
}
/* 我的订单头部CSS */
.order .order-header {
  height: 64px;
  border-bottom: 2px solid #ff6700;
  background-color: #fff;
  margin-bottom: 20px;
}
.order .order-header .order-header-content {
  width: 1225px;
  margin: 0 auto;
}
.order .order-header p {
  font-size: 28px;
  line-height: 58px;
  float: left;
  font-weight: normal;
  color: #424242;
}
/* 我的订单头部CSS END */
.order .content {
  width: 1225px;
  margin: 0 auto;
  background-color: #fff;
  margin-bottom: 50px;
}

.order .content ul {
  background-color: #fff;
  color: #424242;
  line-height: 85px;
}
/* 我的订单表头CSS */
.order .content ul .order-info {
  height: 60px;
  line-height: 60px;
  padding: 0 26px;
  color: #424242;
  border-bottom: 1px solid #ff6700;
}
.order .content ul .order-info .order-id {
  float: left;
  color: #ff6700;
}
.order .content ul .order-info .order-time {
  float: left;
}
.order .content ul .order-info .order-set {
  float: right;
}

.order .content ul .header {
  height: 85px;
  padding-right: 10px;
  color: #424242;
}
/* 我的订单表头CSS END */

/* 订单列表CSS */
.order .content ul .product-list {
  height: 85px;
  padding: 15px 20px 10px 0;
  border-top: 1px solid #e0e0e0;
}
.order .content ul .pro-img {
  float: left;
  height: 85px;
  width: 80px;
  padding-left: 40px;
}
.order .content ul .pro-img img {
  height: 80px;
  width: 80px;
}
.order .content ul .pro-name {
  float: left;
  width: 230px;
}
.order .content ul .pro-address {
  float: left;
  width: 310px;
}
.order .content ul .pro-name a {
  color: #424242;
}
.order .content ul .pro-name a:hover {
  color: #ff6700;
}
.order .content ul .pro-price {
  float: left;
  width: 80px;
  padding-right: 10px;
  text-align: center;
}
.order .content ul .pro-status{
  float: left;
  width: 100px;
  padding-right: 18px;
  text-align: center;
}
.order .content ul .pro-num {
  float: left;
  width: 80px;
  text-align: center;
}
.order .content ul .pro-edit {
  float: left;
  width: 40px;
  padding-right: 21px;
  text-align: center;
}
.order .content ul .pro-edit span:hover{
  cursor: pointer;
  color: #0084ff;
}
.order .content ul .pro-total {
  float: left;
  width: 160px;
  padding-right: 10px;
  text-align: center;
}
.order .content ul .pro-total-in {
  color: #ff6700;
}

.order .order-bar {
  width: 1185px;
  padding: 0 20px;
  border-top: 1px solid #ff6700;
  height: 50px;
  line-height: 50px;
  background-color: #fff;
}
.order .order-bar .order-bar-left {
  float: left;
}
.order .order-bar .order-bar-left .order-total {
  color: #757575;
}
.order .order-bar .order-bar-left .order-total-num {
  color: #ff6700;
}
.order .order-bar .order-bar-right {
  float: right;
}
.order .order-bar .order-bar-right .total-price-title {
  color: #ff6700;
  font-size: 14px;
}
.order .order-bar .order-bar-right .total-price {
  color: #ff6700;
  font-size: 30px;
}
/* 订单列表CSS END */

/* 订单为空的时候显示的内容CSS */
.order .order-empty {
  width: 1225px;
  margin: 0 auto;
}
.order .order-empty .empty {
  height: 300px;
  padding: 0 0 130px 558px;
  margin: 65px 0 0;
  background: url(../assets/imgs/cart-empty.png) no-repeat 124px 0;
  color: #b0b0b0;
  overflow: hidden;
}
.order .order-empty .empty h2 {
  margin: 70px 0 15px;
  font-size: 36px;
}
.order .order-empty .empty p {
  margin: 0 0 20px;
  font-size: 20px;
}
/* 订单为空的时候显示的内容CSS END */
</style>